package com.yl.immodule.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.android.yl.ylicpsdk.ICPRtcEventHandler;
import com.android.yl.ylicpsdk.ICPRtcManager;
import com.camera.base.inerface.CameraServiceSingleton;
import com.camera.base.inerface.FramePusher;
import com.camera.base.inerface.H264Buffer;
import com.camera.base.inerface.ICPInterface;
import com.camera.base.inerface.Pusher;
import com.camera.base.inerface.VideoFrame;
import com.camera.base.inerface.VideoFrameBufferType;
import com.unionbroad.greendao.User;
import com.hm.poc.video.ExternalAudioConsumer;
import com.hm.poc.video.ExternalVideoConsumer;
import com.unionbroad.app.util.Logger;
import com.yl.immodule.R;
import com.yl.immodule.iminterface.MeetManagerInterface;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class MeetWindowManager extends MeetManagerInterface{
    Logger logger = Logger.getLogger("MeetWindowManager");
    long sessionId;
    String remoteId;
    String remoteName;
    int type;
    String extra;
    //来电或外呼
    boolean isIncomingCall;
    ICPRtcManager icpRtcManager = ICPRtcManager.getsInstance();
    public Context mContext;
    public RelativeLayout meetLayout;
    public WindowManager mWindowManager;
    ICPInterface mICPInterface;
    private Handler handler = new Handler();
    LinearLayout llInDialogTip,llOutDialogTip;
    TextView tvInDialogTitle,tvOutDialogTitle;
    Button btnInSure,btnInHanup,btnOut;
    // 私有构造函数，防止实例化
    public MeetWindowManager() {
    }
    @Override
    public void stopMeet(){
        icpRtcManager.removeEventHandler(icpRtcEventHandler);
        icpRtcManager.setExternalAudioConsumer(null);
        icpRtcManager.setExternalVideoConsumer(null);
        removeLayout();
    }
    @Override
    public void startMeet(Context context ,long sessionId,String remoteId,String remoteName,int type,String extra,boolean isIncomingCall){
        Log.e("TAG", "startMeet1111: ");
        setLayout(context);
        icpRtcManager.getConfig();
        icpRtcManager.setExternalAudioConsumer(externalAudioConsumer);
        icpRtcManager.setExternalVideoConsumer(externalVideoConsumer);
        this.sessionId = sessionId;
        this.remoteId = remoteId;
        this.remoteName = remoteName;
        this.type = type;
        this.extra = extra;
        this.isIncomingCall = isIncomingCall;

        llInDialogTip = meetLayout.findViewById(R.id.ll_in_dialog);
        llOutDialogTip = meetLayout.findViewById(R.id.ll_out_dialog);
        tvInDialogTitle = meetLayout.findViewById(R.id.in_dialog_title);
        tvOutDialogTitle = meetLayout.findViewById(R.id.out_dialog_title);
        btnInSure = meetLayout.findViewById(R.id.in_dialog_sure);
        btnInHanup = meetLayout.findViewById(R.id.in_dialog_hanup);
        btnOut = meetLayout.findViewById(R.id.out_dialog_btn);
        btnInSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tryAcceptCall();
            }
        });
        btnInHanup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tryHangUp();
            }
        });
        btnOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tryHangUp();
            }
        });


        boolean isVideoCall = type != ICPRtcEventHandler.SessionType.TYPE_AUDIO_CALL;
        ((ViewGroup) meetLayout.findViewById(R.id.btn_voice).getParent()).setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
        ((ViewGroup) meetLayout.findViewById(R.id.btn_switch_camera).getParent()).setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
        ((ViewGroup) meetLayout.findViewById(R.id.video_call_record).getParent()).setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
        ((ViewGroup) meetLayout.findViewById(R.id.video_call_capture).getParent()).setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
        ((ViewGroup) meetLayout.findViewById(R.id.btn_dispatch).getParent()).setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
        if (icpRtcManager.getSessionType(sessionId) == ICPRtcEventHandler.SessionType.TYPE_AUDIO_MEETING
                || icpRtcManager.getSessionType(sessionId) == ICPRtcEventHandler.SessionType.TYPE_VIDEO_MEETING) {
            ((ViewGroup) meetLayout.findViewById(R.id.btn_query_meeting).getParent()).setVisibility(View.VISIBLE);
            meetLayout.findViewById(R.id.btn_query_meeting).setOnClickListener(v -> {
                icpRtcManager.queryMeetingConfig(Long.valueOf(remoteId), result ->
                        logger.i("queryMeetingConfig.onResponse: " + result));
            });
        }

        //免提
        meetLayout.findViewById(R.id.btn_hangfree).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                icpRtcManager.setEnableSpeakerphone(!icpRtcManager.isSpeakerphoneEnable());
                Toast.makeText(mContext, "当前免提：" + icpRtcManager.isSpeakerphoneEnable(), Toast.LENGTH_SHORT).show();
            }
        });
        //静音
        meetLayout.findViewById(R.id.video_call_mute_btn).setOnClickListener(v -> {
            icpRtcManager.muteMic(!icpRtcManager.isMuteMic());
            Toast.makeText(mContext, "当前静音：" + icpRtcManager.isMuteMic(), Toast.LENGTH_SHORT).show();
        });
        //仅语音(关闭画面推送)
        meetLayout.findViewById(R.id.btn_voice).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 仅是通知对端当前操作，除此之外，还需要移除两个surfaceView
                icpRtcManager.pauseVideo(!icpRtcManager.isVideoOnPause());
                if (icpRtcManager.isVideoOnPause()) {
                    removeLocalVideo();
                    removeRemoteVideo();
                } else {
                    setupLocalVideo();
                    setupRemoteVideo();
                }

                Toast.makeText(mContext, "当前画面关闭：" + icpRtcManager.isVideoOnPause(), Toast.LENGTH_SHORT).show();
            }
        });
        //切换摄像头(如果已经转成仅语音了，应该隐藏这个按钮)
        meetLayout.findViewById(R.id.btn_switch_camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!icpRtcManager.isVideoOnPause()) {
                    icpRtcManager.switchCamera();
                }
                Toast.makeText(mContext, "当前前置摄像头：" + icpRtcManager.isFrontFacingCamera(), Toast.LENGTH_SHORT).show();
            }
        });
        //挂断
        meetLayout.findViewById(R.id.btn_end_call).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tryHangUp();
            }
        });

        //录像
        meetLayout.findViewById(R.id.video_call_record).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isRecording = icpRtcManager.isVideoStreamRecording();
                if (isRecording) {
                    icpRtcManager.stopVideoStreamRecord();
                } else {
                    icpRtcManager.startVideoStreamRecord();
                }
                isRecording = icpRtcManager.isVideoStreamRecording();
                ((TextView) meetLayout.findViewById(R.id.union_toolbar_record_tv)).setText(isRecording ? "录制中..." : "已停止");
            }
        });
        //截屏
        meetLayout.findViewById(R.id.video_call_capture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                icpRtcManager.takeVideoStreamPicture();
            }
        });
        //视频分发
        meetLayout.findViewById(R.id.btn_dispatch).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //========随机取两个联系人======
                List<User> notifyUsers = new ArrayList<>();
                List<User> all = icpRtcManager.getContactsUserList();
                for (User user : all) {
                    if (user.getNumber() == 115 || user.getNumber() == 114) {
                        notifyUsers.add(user);
                    }
                }
                //============================
                icpRtcManager.dispatchVideoEvent(sessionId, notifyUsers);
            }
        });

        //监听必要的事件
        icpRtcManager.addEventHandler(icpRtcEventHandler);

        if (isIncomingCall) {
            if (type == ICPRtcEventHandler.SessionType.TYPE_AUDIO_PTT_TEMP) {
                tvInDialogTitle.setText(remoteName + " 临时组来电");
            } else if (type == ICPRtcEventHandler.SessionType.TYPE_AUDIO_PTT) {
                tvInDialogTitle.setText(remoteName + " 对讲组来电");
            } else {
                tvInDialogTitle.setText(remoteName + " 来电");
            }
            llInDialogTip.setVisibility(View.VISIBLE);
        } else {
            boolean ret = makeCall(remoteId, type);
            if (!ret) {
                stopMeet();//隐藏
            }
        }

        //大小屏切换
        meetLayout.findViewById(R.id.local_video_view_container).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View fromSmall = ((ViewGroup) meetLayout.findViewById(R.id.local_video_view_container)).getChildAt(0);
                View fromBig = ((ViewGroup) meetLayout.findViewById(R.id.remote_video_view_container)).getChildAt(0);

                ((ViewGroup) meetLayout.findViewById(R.id.local_video_view_container)).removeAllViews();
                ((ViewGroup) meetLayout.findViewById(R.id.remote_video_view_container)).removeAllViews();

                ViewGroup.LayoutParams bigLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                ViewGroup.LayoutParams smallLayoutParams =
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT);
                ((ViewGroup) meetLayout.findViewById(R.id.local_video_view_container)).addView(fromBig, bigLayoutParams);
                ((ViewGroup) meetLayout.findViewById(R.id.remote_video_view_container)).addView(fromSmall, smallLayoutParams);
            }
        });

        view2WindowManger(true);
    }
    public void setLayout(Context context){
        this.mContext = context;
        if (meetLayout == null){
            mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
            meetLayout = (RelativeLayout)View.inflate(context, R.layout.activity_av2, null);
        }
    }
    public void removeLayout(){
        view2WindowManger(false);
    }
    /**
     * @param isAddView 全屏
     * 更新整个Service UI to WindowManager
     */
    public void view2WindowManger(Boolean isAddView) {
        handler.post(new Runnable() {
            @SuppressLint("WrongConstant")
            @Override
            public void run() {
                if (meetLayout != null) {
                    if (isAddView){
                        WindowManager.LayoutParams layoutParams  = new WindowManager.LayoutParams();
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //6.0
                            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                        } else {
                            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
                        }
                        layoutParams.flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH);
                        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
                        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
                        layoutParams.flags = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
                        layoutParams.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
                        layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
                        mWindowManager.addView(meetLayout, layoutParams);
                    }else{
                        mWindowManager.removeView(meetLayout);
                        meetLayout = null;
                        mWindowManager = null;
                    }
                }
            }
        });
    }
    private final ExternalAudioConsumer externalAudioConsumer = new ExternalAudioConsumer() {
        @Override
        public void onAudioFrame(long sessionId, byte[] audioBytes, long dataLen) {
            //logger.i("onAudioFrame: " + dataLen);
        }
    };

    private final ExternalVideoConsumer externalVideoConsumer = new ExternalVideoConsumer() {
        @Override
        public void onVideoFrame(long sessionId, ByteBuffer byteBuffer) {
            logger.i("onVideoFrame: " + byteBuffer);
        }
    };

    final ICPRtcEventHandler icpRtcEventHandler = new ICPRtcEventHandler() {

        @Override
        public void onRemoteRinging(long sessionId, long uid) {
            tvInDialogTitle.setText("远端正在响铃: " + uid);
        }

        @Override
        public void onCallConnected(long sessionId, String uid, int sessionType) {
            if (MeetWindowManager.this.sessionId == sessionId) {
                logger.d(">>>>>>onCallConnected sessionId=" + sessionId + " uid=" + uid + " sessionType=" + sessionType);
                MeetWindowManager.this.sessionId = sessionId;
                /** 如果是一个视频呼叫，一般远端画面全屏，本地画面右上角小窗口显示 */
                if (sessionType == SessionType.TYPE_VIDEO_CALL ||
                        sessionType == SessionType.TYPE_VIDEO_MEETING) {
                    setupLocalVideo();
                    setupRemoteVideo();
                }
                /** 如果是一个视频回传呼叫、直播、报警呼叫，是没有远端画面的，把本地画面全屏显示即可 */
                else if (sessionType == SessionType.TYPE_VIDEO_MONITOR_CALL ||
                        sessionType == SessionType.TYPE_VIDEO_SOS_CALL ||
                        sessionType == SessionType.TYPE_VIDEO_LIVE) {
                    setupMonitorVideo();
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        llOutDialogTip.setVisibility(View.GONE);
                        llInDialogTip.setVisibility(View.GONE);

                        //工具建立通话成功后，才显示
                        meetLayout.findViewById(R.id.call_toolbar_layout).setVisibility(View.VISIBLE);
                        meetLayout.findViewById(R.id.call_toolbar2_layout).setVisibility(View.VISIBLE);
                    }
                });
            }
        }

        @Override
        public void onCallDisconnected(long sessionId, int reason) {
            if (MeetWindowManager.this.sessionId == sessionId) {
                logger.d(">>>>>>onCallDisconnected sessionId=" + sessionId + " reason=" + reason);
                if (reason == CallEndReason.TERMINATED) {
                    Toast.makeText(mContext, "挂断", Toast.LENGTH_SHORT).show();
                } else if (reason == CallEndReason.REMOTE_BUSY) {
                    Toast.makeText(mContext, "对方繁忙", Toast.LENGTH_SHORT).show();
                } else if (reason == CallEndReason.REMOTE_OFFLINE) {
                    Toast.makeText(mContext, "对方离线", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(mContext, "未定义", Toast.LENGTH_SHORT).show();
                }
                llOutDialogTip.setVisibility(View.GONE);
                llInDialogTip.setVisibility(View.GONE);
                stopMeet();
            }
        }

        @Override
        public void onIgnoreIncoming(int type, long callerId) {
            //当前已有会话，新的来电被自动拒绝
            Toast.makeText(mContext, "有来电被拒绝"+callerId, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRemoteVideoPause(boolean pause) {
            if (pause) {
                //去掉两个SurfaceView，并显示一张图片
                removeLocalVideo();
                removeRemoteVideo();
            } else {
                //重新显示SurfaceView
                setupRemoteVideo();
                setupLocalVideo();
            }
        }

        @Override
        public void onVideoStreamRecordFinish(String filePath) {
            if (TextUtils.isEmpty(filePath)) {
                Toast.makeText(mContext, "录制失败", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(mContext, "文件保存到：" + filePath, Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onVideoStreamTakePictureFinish(String filePath) {
            Toast.makeText(mContext, "拍照成功->" + filePath, Toast.LENGTH_SHORT).show();
        }
    };
    private void getCameraInterface() {
        mICPInterface = CameraServiceSingleton.INSTANCE.getInstance();
    }
    MFramePusher pusher;
    public class MFramePusher extends FramePusher {
        public MFramePusher(int type) {
            super(type);
        }
        @Override
        public void onFrame(VideoFrame farme) {
            if (farme.getBuffer().getBufferType() == VideoFrameBufferType.H264_PCM_BUFFER && ((H264Buffer) farme.getBuffer()).getData() != null) {
                icpRtcManager.pushVideoFrame(sessionId,((H264Buffer) farme.getBuffer()).getData() ,((H264Buffer) farme.getBuffer()).getData().length);
            }
        }
    }
    private void setupLocalVideo() {
        FrameLayout container = (FrameLayout) meetLayout.findViewById(R.id.local_video_view_container);

        // ===for debug start
        container.setPadding(5, 5, 5, 5);
        container.setBackgroundColor(Color.RED);
        // ===for debug end

        container.setVisibility(View.VISIBLE);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
        getCameraInterface();
        Log.e("TAG", "setupLocalVideo: " +(mICPInterface != null));
        if (mICPInterface != null){
            mICPInterface.openICPCamera();
        }

//        View surfaceView = icpRtcManager.getRendererView(ICPRtcEventHandler.SurfaceType.LOCAL_SURFACE_VIEW);
        SurfaceView mSurfaceView = new SurfaceView(mContext);
        mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
                if (mICPInterface != null){
                    mICPInterface.addOtherSurface(surfaceHolder.getSurface(),100,100);


//                    pusher = new Pusher() {
//                        @Override
//                        public void onPush(byte[] data, boolean isVideo, boolean isSync, long timeS, int state,int width,int height) {
//                            icpRtcManager.pushVideoFrame(sessionId,data,data.length);
//                        }
//                    };
                    pusher = new MFramePusher(VideoFrameBufferType.H264_PCM_BUFFER);
                    mICPInterface.addSubcodeStreamPusher(pusher);
                    mICPInterface.requestKeyFrame(5);
                }

            }

            @Override
            public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
                if (mICPInterface != null){
                    mICPInterface.removeSubcodeStream(pusher);
                    mICPInterface.removeOtherSurface(surfaceHolder.getSurface());
                }
            }
        });
        if (mSurfaceView != null) {
            container.addView(mSurfaceView);
            icpRtcManager.sendLocalVideo();
        } else {
            logger.d("get local surface view null");
        }
    }
    private void setupRemoteVideo() {
        FrameLayout container = (FrameLayout) meetLayout.findViewById(R.id.remote_video_view_container);
        container.setVisibility(View.VISIBLE);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
        View surfaceView = icpRtcManager.getRendererView(ICPRtcEventHandler.SurfaceType.REMOTE);
        if (surfaceView != null) {
            container.addView(surfaceView);
            container.bringChildToFront(surfaceView);
        } else {
            logger.d("get remote surface view null");
        }
    }
    private void removeLocalVideo() {
        FrameLayout container = (FrameLayout) meetLayout.findViewById(R.id.local_video_view_container);
        container.setVisibility(View.GONE);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
    }
    private void removeRemoteVideo() {
        FrameLayout container = (FrameLayout) meetLayout.findViewById(R.id.remote_video_view_container);
        container.setVisibility(View.VISIBLE);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }

        //一般需要显示一张表示当前为语音通话的图片
        View tipView = new View(mContext);
        tipView.setBackgroundResource(R.drawable.bg_audio_only);
        tipView.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        container.addView(tipView);
    }
    private boolean makeCall(String id, int sessionType) {
        if (TextUtils.isEmpty(id)) {
            Toast.makeText(mContext, "无效id", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!icpRtcManager.hasServiceConnected()) {
            Toast.makeText(mContext, "服务连接已断开", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (sessionType == ICPRtcEventHandler.SessionType.TYPE_VIDEO_CALL) {
            sessionId = icpRtcManager.makeVideoCall(id + "", "args");
        } else if (sessionType == ICPRtcEventHandler.SessionType.TYPE_VIDEO_MONITOR_CALL
                || sessionType == ICPRtcEventHandler.SessionType.TYPE_AUDIO_MONITOR_CALL) {
            sessionId = icpRtcManager.makeVideoMonitorCall(id);
        } else if (sessionType == ICPRtcEventHandler.SessionType.TYPE_VIDEO_SOS_CALL) {
            sessionId = icpRtcManager.makeVideoSOSCall(id);
        } else if (sessionType == ICPRtcEventHandler.SessionType.TYPE_VIDEO_MEETING) {
            sessionId = icpRtcManager.makeVideoMeeting(id);
        }else if (sessionType == ICPRtcEventHandler.SessionType.TYPE_AUDIO_MEETING) {
            sessionId = icpRtcManager.makeAudioMeeting(id);
        } else {
            sessionId = icpRtcManager.makeAudioCall(id + "", "args");
        }
        if (sessionId > 0) {
            llOutDialogTip.setVisibility(View.VISIBLE);
            tvOutDialogTitle.setText("连接中");
            llInDialogTip.setVisibility(View.GONE);
        }
        return sessionId > 0;
    }
    /**
     * 点对点接听 和 对讲组接听
     */
    @Override
    public void tryAcceptCall() {
        if (type == ICPRtcEventHandler.SessionType.TYPE_AUDIO_PTT || type == ICPRtcEventHandler.SessionType.TYPE_AUDIO_PTT_TEMP) {
            boolean ret = icpRtcManager.joinChannel(remoteId);
            if (!ret) {
                Toast.makeText(mContext, "接听失败：" + sessionId, Toast.LENGTH_SHORT).show();
                tryHangUp();
            }
            stopMeet();
            //对讲组接听成功后，要退出来电界面，如果是对讲组来电，一般使用全局dialog就好，这里跳到AvActivity只是为了方便
        } else {
            boolean ret = icpRtcManager.acceptCall(sessionId);
            if (!ret) {
                Toast.makeText(mContext, "接听失败：" + sessionId, Toast.LENGTH_SHORT).show();
                stopMeet();
            }
        }
    }
    @Override
    public void tryHangUp() {
        icpRtcManager.hangUpCall(sessionId);
        stopMeet();
    }
    private void setupMonitorVideo() {
        meetLayout.findViewById(R.id.local_video_view_container).setVisibility(View.GONE);
        FrameLayout container = (FrameLayout) meetLayout.findViewById(R.id.remote_video_view_container);
        container.setVisibility(View.VISIBLE);
        if (container.getChildCount() > 0) {
            container.removeAllViews();
        }
        View surfaceView = icpRtcManager.getRendererView(ICPRtcEventHandler.SurfaceType.LOCAL);
        if (surfaceView != null) {
            container.addView(surfaceView);
            icpRtcManager.sendLocalVideo();
        } else {
            logger.d("get local surface view null");
        }
    }
}
